package com.jahe.learn.netty4.zerocopy;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 *  nio的机制传送文件数据，使用零拷贝机制。
 * @author: jahe.lai
 * @create: 2019-12-25 20:13
 */
public class NioFileServer {

    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//        serverSocketChannel.configureBlocking(false);

        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.setReuseAddress(true);

        InetSocketAddress address = new InetSocketAddress(8899);
        serverSocketChannel.bind(address);

        ByteBuffer buffer = ByteBuffer.allocate(4096);

        while (true) {
            System.out.println("1");
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(true);

            int readcount = 0;
            while (-1 != readcount) {
                readcount = socketChannel.read(buffer);
                buffer.rewind();
            }
        }
    }
}
